/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.test.helper;

import java.lang.reflect.InvocationTargetException;

import org.apache.commons.beanutils.PropertyUtils;
import org.junit.Test;

/**
 * This class is a base class to test bean. It will automatically tests some
 * properties that all bean must verify.
 */
public abstract class AbstractBeanTester
{

  /**
   * the values used to test the beans.
   */
  private final Object[] valuesToTest;



  /**
   * constructor.
   * 
   * @param valuesToTest
   *          the values used to test the beans
   */
  protected AbstractBeanTester(Object[] valuesToTest)
  {
    this.valuesToTest = valuesToTest;
  }



  /**
   * Returns the bean to test.
   * 
   * @return the bean to test
   */
  public abstract Object getNewObjectToTest();



  /**
   * Returns the list of all the string properties of the bean that must be
   * tested.
   * 
   * @return the list of properties of the bean to test
   */
  public abstract String[] getStringPropertiesToTest();



  /**
   * This method tests that for any bean, any property and the values to test
   * defined in <code>valuesToTest</code>, we have the following property :
   * <code>bean.getProperty()</code> does not change after
   * <code>bean.setProperty(bean.getProperty())</code>.
   * 
   * @throws NoSuchMethodException
   *           when there is no getter or setter for a property
   * @throws InvocationTargetException
   *           when there is a problem accessing a property
   * @throws IllegalAccessException
   *           when the getter or setter of a property is not
   *           <code>public</code>
   */
  @Test
  public void doTest()
      throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
  {

    Object objectToTest = getNewObjectToTest();
    for (String property : getStringPropertiesToTest()) {
      Object newValue = PropertyUtils.getProperty(objectToTest, property);
      PropertyUtils.setProperty(objectToTest, property, newValue);
      org.junit.Assert.assertEquals(newValue, PropertyUtils.getProperty(objectToTest, property));
      for (Object value : valuesToTest) {
        PropertyUtils.setProperty(objectToTest, property, value);
        newValue = PropertyUtils.getProperty(objectToTest, property);
        PropertyUtils.setProperty(objectToTest, property, newValue);
        org.junit.Assert.assertEquals(newValue, PropertyUtils.getProperty(objectToTest, property));
      }

    }

  }

}
